﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Command;
using JustinAngel.Neurons.AppCore;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Navigation;
using JustinAngel.Neurons.ServerModel;
using JustinAngel.Neurons.Services;
using System.Linq;

namespace JustinAngel.Neurons.Pages.Main
{
    public class MainPageViewModel : VViewModelBase
    {
        public MainPageViewModel()
        {
            if (!IsInDesignMode)
            {
                // massive perf optimization for responsive loading
                DispatcherTimerHelper.On(TimeSpan.FromMilliseconds(100), () =>
                new Thread(() => WithRssRootItem(root => PopulateItemCollections(root))).Start());
            }
        }

        public ICommand Loaded
        {
            get
            {
                return new RelayCommand(() =>
                                            {
                                                //IsLoading = true;
                                            });
            }
        }

        //public ICommand Loaded
        //{
        //    get
        //    {
        //        return new RelayCommand(() =>
        //            WithRssRootItem(root =>
        //             DispatcherTimerHelper.OnWithDispatcher(TimeSpan.FromMilliseconds(200), () =>
        //                 PopulateItemCollections(root))));
        //    }
        //}

        private void PopulateItemCollections(RssRootItem obj)
        {
            RSAItems = obj.Items
                          .Where(rssVideoItem => rssVideoItem.IsRSA)
                          .Select(rssVideoItem => rssVideoItem.ToVideoItemViewModel)
                          .ToList();
            TEDItems = obj.Items
                          .Where(rssVideoItem => rssVideoItem.IsTED)
                          .Select(rssVideoItem => rssVideoItem.ToVideoItemViewModel)
                          .ToList();

            ScienceDumpItems = obj.Items
                          .Where(rssVideoItem => rssVideoItem.IsScienceDump)
                          .Select(rssVideoItem => rssVideoItem.ToVideoItemViewModel)
                          .ToList();

            FORAtvItems = obj.Items
                          .Where(rssVideoItem => rssVideoItem.IsForaTV)
                          .Select(rssVideoItem => rssVideoItem.ToVideoItemViewModel)
                          .ToList();

#if DEBUG
            ScienceDumpItems.Insert(0, new VideoItemViewModel(
                new Uri("http://img.youtube.com/vi/6Tn3-eB_hcQ/0.jpg"),
                new Uri("http://www.youtube.com/watch?v=6Tn3-eB_hcQ"),
                new Uri("http://www.youtube.com/watch?v=6Tn3-eB_hcQ"),
                "Stephen Fry at the Microsoft UK Windows Phone Launch in London",
                "ScienceDump",
                new Uri("http://img.youtube.com/vi/6Tn3-eB_hcQ/0.jpg")));
#endif

                // massive perf optimization for responsive loading

            BeginInvokeOnUIThread(UpdateAllVisibleCollections);
        }

        public void UpdateAllVisibleCollections()
        {
            RaisePropertyChanged(() => RSAItems);
            RaisePropertyChanged(() => TEDItems);
            RaisePropertyChanged(() => ScienceDumpItems);
            RaisePropertyChanged(() => FORAtvItems);
            IsLoading = false;

        }

        private bool _isLoading;
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                _isLoading = value;
                RaisePropertyChangedAndUpdateVSM(() => IsLoading, value);
            }
        }

        public List<VideoItemViewModel> RSAItems { get; set; }
        public List<VideoItemViewModel> TEDItems { get; set; }
        public List<VideoItemViewModel> ScienceDumpItems { get; set; }
        public List<VideoItemViewModel> FORAtvItems { get; set; }

        [Dependency]
        public ITaskInvoker TaskInvoker { get; set; }

        public ICommand SendEmailToDeveloper
        {
            get
            {
                return new RelayCommand(() =>
                    TaskInvoker.SendEmail("J@JustinAngel.net",
                                          "Neurons V1.0 feedback",
                                          "Hi Justin, I'm using Neurons V1.0 and I'd just like to say that...")
                );
            }
        }

        public ICommand GoToWebsite
        {
            get
            {
                return new RelayCommand<string>((arg) =>
                    TaskInvoker.OpenWebBrowser(arg));
            }
        }

        public ICommand NavigateToSearch
        {
            get
            {
                return new RelayCommand<string>((e) => 
                    DispatcherTimerHelper.On(TimeSpan.FromMilliseconds(100),
                    () => MessengerInstance.Send(new NavigateToMessage(PageSources.SearchPage, e))));
            }
        }
    }
}
